/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName CustomGridActivityListItemRenderer.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.views.renderers
{

	import com.gesturesoft.imultimax.model.AppModel;
	import com.gesturesoft.imultimax.model.utility.DateUtil;
	
	import flash.display.CapsStyle;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.text.StyleSheet;
	
	import mx.core.FlexGlobals;
	import mx.core.IDataRenderer;
	import mx.core.mx_internal;
	import mx.events.EffectEvent;
	import mx.events.PropertyChangeEvent;
	import mx.formatters.DateFormatter;
	import mx.graphics.BitmapScaleMode;
	import mx.styles.CSSStyleDeclaration;
	
	import spark.components.Label;
	import spark.components.LabelItemRenderer;
	import spark.components.supportClasses.StyleableTextField;

	use namespace mx_internal;


	/**
	 * Constructor
	 */
	public class CustomGridActivityListItemRenderer extends LabelItemRenderer implements IDataRenderer
	{

		//
		// Constants
		//
		public static const H_GAP:int=4;

		//
		// UI Bits
		//
		protected var toFromDisplay:StyleableTextField;
		protected var contactDisplay:StyleableTextField;


		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		/**
		 * Toggle View
		 * 
		 */
		
		private var _isSelected:Boolean;
		[Bindable]
		public function get isSelected():Boolean
		{
			
			return this._isSelected;
			
		}
		
		public function set isSelected(value:Boolean):void
		{
			
			this._isSelected = value;
			
		}

		/**
		 * itemContent
		 * 
		 */
		
		private var _itemContent:Object;

		[Bindable]
		public function get itemContent():Object
		{
			
			return _itemContent;
			
		}

		public function set itemContent(value:Object):void
		{
			
			_itemContent=value;
			
			if (labelDisplay)
				labelDisplay.text= getformattedDate(value.act_date);

			toFromDisplay.text = itemContent.act_activity;
			contactDisplay.text = itemContent.act_result;
			
		}

		/**
		 * data
		 * Required by IDataRenderer
		 */
		
		override public function set data(value:Object):void
		{
			
			super.data=value;

			if (data)
				itemContent= value;
			
		}

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		public function CustomGridActivityListItemRenderer()
		{
			
			super();
			
		}

		//--------------------------------------------------------------------------
		//
		//  Overridden methods: LabelItemRenderer
		//
		//--------------------------------------------------------------------------

		// The message field is used to display the name of the it.  Customize its 
		// appearance by overriding its creation methods
		override protected function createLabelDisplay():void
		{
			
			labelDisplay = StyleableTextField(createInFontContext(StyleableTextField));
			labelDisplay.styleName=this;
			labelDisplay.editable=false;
			labelDisplay.selectable=false;
			labelDisplay.cacheAsBitmap=true;
			labelDisplay.multiline=true;
			labelDisplay.wordWrap=true;
			labelDisplay.cacheAsBitmap=true;
			labelDisplay.setStyle("fontSize","12");
			labelDisplay.setStyle("fontWeight","bold");
			labelDisplay.setStyle("color",0x525252);

			if (data && data.date )
				labelDisplay.text=getformattedDate(data.act_date);

			addChild(labelDisplay);
			
		}
		
		private function getformattedDate(value:String):String
		{
			
			if(!value)
				return "";
			
			var time:String = getTime( value );
			value = value.substr(0,10);
			var array:Array = value.split("-");
			value = array[0].toString() + "-" + DateUtil.getShortMonth(array[1].toString()) + "-" + array[2].toString();
			
			value += unescape("\n"+time);
			
			return value;
			
		}
		
		private function getTime(val:String):String
		{
			
			var hour:Number = 0;
			var minute:String;
			var ampm:String = null;
			var sec:String;
			var time:String;
			
			minute = val.substr(14, 2);
			sec = val.substr(17, 2);
			
			if(AppModel.getInstance().isDevice24HourFormat != true)
			{
				
				hour = Number( val.substr(11, 2) );
				if( hour > 12 )
				{
					
					ampm = " PM";
					hour = Number(val.substr(11, 2)) - 12;
					
				}
				else
				{
					
					ampm = " AM";
					
				}
				
				
				time = hour+":"+minute+":"+sec+ampm;
				
			}
			else
			{
				
				time = val.substring(11);
				
			}
			
			return time;
			
		}
		

		override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
			if (!(selected || down))
			{
				
				if(itemIndex%2 == 0)
				{
					
					graphics.beginFill(0xFFFFFF);
					graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
					graphics.endFill();
					graphics.beginFill(0xeeeeee);
					graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
					graphics.endFill();
					
				}
				else
				{
					
					graphics.beginFill(0xFFFFFF);
					graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
					graphics.endFill();
					graphics.beginFill(0xf7f7f7);
					graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
					graphics.endFill();
					
				}
				
			}
			
			// Draw our own color divider
			graphics.lineStyle(1, 0xdedede, 1, false, "normal", CapsStyle.NONE);
			graphics.moveTo(0, unscaledHeight - 1);
			graphics.lineTo(unscaledWidth, unscaledHeight - 1);
			
		}

		override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
			// Update styles on textFields
			labelDisplay.commitStyles();
			toFromDisplay.commitStyles();
			contactDisplay.commitStyles();

			// labelDisplay
			var labelHeight:Number=getElementPreferredHeight(labelDisplay);
			var labelY:Number = 10;
			var itemWidth:Number = Math.floor(unscaledWidth/4) - 4;
			
			setElementSize(labelDisplay, itemWidth, labelHeight);
			setElementPosition(labelDisplay, H_GAP, (unscaledHeight/2)-(labelHeight/2));
			
			// toFromDisplay
			var toFromHeight:Number=getElementPreferredHeight(toFromDisplay);
			setElementSize(toFromDisplay, unscaledWidth-itemWidth, toFromHeight);
			setElementPosition(toFromDisplay, itemWidth + H_GAP, labelY);
			
			// contactDisplay
			var contactHeight:Number=getElementPreferredHeight(contactDisplay);
			setElementSize(contactDisplay, itemWidth, contactHeight);
			setElementPosition(contactDisplay, itemWidth + H_GAP, labelY + contactHeight + H_GAP);
			
			// inDisplay
			
			// Invailidate the size so that we can revalidate text due to potential reflow
			if (labelHeight != getElementPreferredHeight(labelDisplay))
				invalidateSize();
			
		}

		//--------------------------------------------------------------------------
		//
		//  Overridden methods: UIComponent
		//
		//--------------------------------------------------------------------------

		override protected function createChildren():void
		{
			
			super.createChildren();

			if (!toFromDisplay)
			{
				
				toFromDisplay = StyleableTextField(createInFontContext(StyleableTextField));
				toFromDisplay.styleName=this;
				toFromDisplay.editable=false;
				toFromDisplay.selectable=false;
				toFromDisplay.multiline=false;
				toFromDisplay.wordWrap=false;
				toFromDisplay.cacheAsBitmap=true;
				toFromDisplay.setStyle("fontSize","13");
				toFromDisplay.setStyle("fontWeight","bold");
				toFromDisplay.setStyle("color",0x525252);

				addChild(toFromDisplay);

				// Set data if avaiable
				if (itemContent && itemContent.act_activity)
				{
					
					toFromDisplay.text = itemContent.act_activity;
					
				}
				
			}
			
			if (!contactDisplay)
			{
				
				contactDisplay = StyleableTextField(createInFontContext(StyleableTextField));
				contactDisplay.styleName=this;
				contactDisplay.editable=false;
				contactDisplay.selectable=false;
				contactDisplay.multiline=false;
				contactDisplay.wordWrap=true;
				contactDisplay.cacheAsBitmap=true;
				contactDisplay.setStyle("fontSize","10");
				contactDisplay.setStyle("fontWeight","bold");
				contactDisplay.setStyle("color",0x525252);

				addChild(contactDisplay);

				// Set data if avaiable
				if (itemContent && itemContent.act_result)
				{
					
					contactDisplay.text = itemContent.act_result;
					
				}
				
			}
			
		}
		
		override protected function measure():void
		{
			
			var calculatedHeight:Number= getElementPreferredHeight(contactDisplay);
			
			// Round
			calculatedHeight=Math.floor(calculatedHeight - 8);
			
			// Minimum height of 14 pixels
			measuredHeight = calculatedHeight;
			
			// Measured width
			measuredWidth = this.parent.width;
			
			// Minimums , Hard code in the measuredMinWidth
			measuredMinWidth=310;
			measuredMinHeight=measuredHeight;
			
		}
		
	}
	
}
